package com.codeoss.framework.view.renderer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.report.util.SummaryReportModel;
import com.codeoss.framework.report.util.SummaryReportUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.report.SummaryReport;
import com.codeoss.module.ir.model.IrReport;
import com.codeoss.module.ir.service.impl.IrReportService;

@Service("summaryreportRenderer")
public class SummaryReportRenderer extends CommonComponentRenderer {
	@Autowired
	private IrReportService irReportService;
	@Autowired
	private SummaryReportUtil summaryReportUtil;
	@Override
	protected void startRenderView(YunContext yunContext, Component component, ViewSerializeMap viewSerializeMap) {
		super.startRenderView(yunContext, component, viewSerializeMap);
		SummaryReport summaryReport=(SummaryReport)component;
		viewSerializeMap.put("reportCode", summaryReport.getReportCode());
		
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", summaryReport.getReportCode());
		IrReport irReport=irReportService.getOne(query);
		if(irReport!=null) {
			viewSerializeMap.put("title", irReport.getName());
		}
	}
	
	@Override
	protected PropSerializeMap startRenderProp(YunContext yunContext,
			Component component, ViewPropMap viewPropMap) {
		PropSerializeMap propSerializeMap=super.startRenderProp(yunContext, component, viewPropMap);
		SummaryReport summaryReport=(SummaryReport)component;
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", summaryReport.getReportCode());
		IrReport irReport=irReportService.getOne(query);
		String jsonContent=irReport.getJsonContent();
		com.codeoss.framework.report.def.SummaryReport report=null;
		if(!StringUtils.isEmpty(jsonContent)) {
			report=summaryReportUtil.buildSummaryReportFromIrReport(irReport);
		}
		SummaryReportModel summaryReportModel=summaryReportUtil.buildSummaryReportData(report);
		propSerializeMap.put("summaryReportModel", summaryReportModel);
		return propSerializeMap;
	}
}
